package fi.hut.soberit.agilefant.reporting.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sun.jersey.api.client.WebResource;

import fi.hut.soberit.agilefant.model.export.Iteration;
import fi.hut.soberit.agilefant.model.export.Product;
import fi.hut.soberit.agilefant.model.export.Project;
import fi.hut.soberit.agilefant.reporting.communication.RemoteCommunicationBuilder;

@Controller
public class BacklogDetailsController {
  
  @Autowired
  private RemoteCommunicationBuilder rcb;
  
  @RequestMapping("product.htm")
  public ModelAndView getProduct(@RequestParam("id") Integer productId) {
    ModelAndView mv = new ModelAndView("backlogDetails");
    WebResource res = rcb.getResourceForUrl("/product/" + productId);
    
    Product product = res.get(Product.class);
    
    mv.addObject("backlog", product);
    return mv;
  }
  
  @RequestMapping("project.htm")
  public ModelAndView getProject(@RequestParam("id") Integer projectId) {
    ModelAndView mv = new ModelAndView("backlogDetails");
    WebResource res = rcb.getResourceForUrl("/project/" + projectId);
    
    Project project = res.get(Project.class);
    
    mv.addObject("backlog", project);
    return mv;
  }
  
  @RequestMapping("iteration.htm")
  public ModelAndView getIteration(@RequestParam("id") Integer iterationId) {
    ModelAndView mv = new ModelAndView("backlogDetails");
    WebResource res = rcb.getResourceForUrl("/iteration/" + iterationId);
    
    Iteration iteration = res.get(Iteration.class);
    
    mv.addObject("backlog", iteration);
    return mv;
  }
}
